/*
 * @Author: ailin
 * @Date: 2022-11-26 15:09:55
 * @LastEditors: ailin
 * @LastEditTime: 2023-09-01 16:29:22
 * @Description: file content
 * @FilePath: /pay_platform/vite.config.js
 */

import vue from "@vitejs/plugin-vue"
import Components from "unplugin-vue-components/vite"
import { VantResolver } from "unplugin-vue-components/resolvers"
import { fileURLToPath, URL } from "node:url"
import { defineConfig, loadEnv, ConfigEnv } from "vite"
import viteCompression from "vite-plugin-compression"
import eslintPlugin from "vite-plugin-eslint"
import * as path from "path"
import legacy from "@vitejs/plugin-legacy"
export default ({ command, mode }) => {
  //参数mode为开放模式或生产模式
  //command:serve开发环境，build线上环境
  //mode:beta、testing、prd环境
  console.log(mode, command) // development or product
  const env = loadEnv(mode, process.cwd()) // 获取.env文件里定义的环境变量
  console.log(env) //变量在命令行里打印出来
  return defineConfig({
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url))
      }
      // extensions: [".js", ".vue"]
    },
    plugins: [
      vue(),
      legacy({
        targets: ["defaults", "not IE 11"]
      }),

      // gzip压缩 生产环境生成 .gz 文件
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: "gzip",
        ext: ".gz"
      }),
      // 配置eslintPlugin
      eslintPlugin({
        cache: false,
        include: /\.(jsx?|tsx?|vue|svelte)$/
      }),
      Components({
        resolvers: [VantResolver()]
      })
    ],

    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/assets/css/mixin.scss";`
        }
      }
    },
    server: {
      open: false,
      cors: true,
      port: 3001,
      host: "localhost",
      hmr: true //是否打开热更新
    },
    //项目部署在主域名的子文件使用,例如http://localhost:3000/myvite/。不填默认就是/
    // base: env.VITE_APP_BASE_URL || "/",
    //静态资源服务的文件夹
    //  publicDir: "public",
    base: "./",
    //生产环境打包配置
    //去除 console debugger
    build: {
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: mode === "production",
          drop_debugger: true
        }
      }
    }
  })
}

// export default defineConfig({
//   plugins: [
//     vue(),
//     Components({
//       resolvers: [VantResolver()]
//     })
//   ],
//   resolve: {
//     alias: {
//       "@": fileURLToPath(new URL("./src", import.meta.url))
//     }
//   }
// })
